from django.shortcuts import render
from rest_framework.views import APIView
from luffyapi.utils.response import APIResponse
from rest_framework.mixins import ListModelMixin
from rest_framework.generics import GenericAPIView
from rest_framework.viewsets import GenericViewSet
from . import models
from . import serializer
from django.conf import settings
from django.core.cache import cache
from rest_framework.response import Response


# class BannerView(GenericAPIView, ListModelMixin):
# 路由配置： path('banner/', views.BannerView.as_view()),
class BannerView(GenericViewSet, ListModelMixin):
    """
    from . import views
    from rest_framework.routers import SimpleRouter

    router = SimpleRouter()
    router.register('banner', views.BannerView, 'banner')

    path('', include(router.urls))
    """
    # 最多展示3条数据
    queryset = models.Banner.objects.filter(
        is_delete=False,
        is_show=True
    ).order_by('orders')[:settings.BANNER_COUNTER]
    serializer_class = serializer.BannerModelSerializer

    def list(self, request, *args, **kwargs):
        # response = super().list(request, *args, **kwargs)
        # 把data的数据加缓存
        # 1 先去缓存拿数据
        banner_list = cache.get('banner_list')
        if not banner_list:  # 缓存中没有，去数据库拿
            print('走数据库')
            response = super().list(request, *args, **kwargs)
            # 加到缓存
            cache.set('banner_list', response.data, 60*60*24)
            return response
        return Response(data=banner_list)


class TestViews(APIView):
    def get(self, request, *args, **kwargs):
        dic = {'name': 'huanggang'}
        # print(dic['age'])
        return APIResponse(dic)
